<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function curl_send($url, $data,$username,$password)
{
    $basic_str = $username.':'.$password;
    $data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //设置头部信息
    $headers = array(
        'Content-Type:application/json; charset=utf-8',
        'Content-Length: '.strlen($data),
        'Authorization: Basic '. base64_encode("$basic_str")
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //执行请求
    $output = curl_exec($ch);

    curl_close($ch);

    return $output;
}

function patchurl($url,$data,$username,$password){
    $basic_str = $username.':'.$password;
    $data  = json_encode($data);
    $ch = curl_init();
    curl_setopt ($ch,CURLOPT_URL,$url);
    //设置头部信息
    $headers = array(
        'Content-Type:application/json; charset=utf-8',
        'Authorization: Basic '. base64_encode("$basic_str")
    );
    curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);     //20170611修改接口，用/id的方式传递，直接写在url中了
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output,true);
    return $output;
}

function geturl($url){
    $headerArray =array("Content-type:application/json;","Accept:application/json");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($url,CURLOPT_HTTPHEADER,$headerArray);
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output,true);
    return $output;
}


function puturl($url,$data,$username,$password){
    $data = json_encode($data);
    $ch = curl_init(); //初始化CURL句柄
    curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
    //设置头部信息
    $basic_str = $username.':'.$password;
    $headers = array(
        'Content-Type:application/json; charset=utf-8',
        'Authorization: Basic '. base64_encode("$basic_str")
    );
    curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串，而不是直接输出
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;

}

function delurl($url,$data){
    $data  = json_encode($data);
    $ch = curl_init();
    curl_setopt ($ch,CURLOPT_URL,$url);
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output,true);
}

function save_apilog($msg,$advname)
{
    $path = RUNTIME_PATH . 'apilog/'.$advname;
    if (! is_dir($path)) {
        mkdir($path,0777,true);
    }
    $filename = $path . '/' . date('YmdH') . '.log';
    $content = date("Y-m-d H:i:s")."\r\n".$msg."\r\n \r\n \r\n ";
    file_put_contents($filename, $content, FILE_APPEND);
}

function curl_upload($url,$path)
{
    $curl = curl_init();
    if (class_exists('\CURLFile')) {
        curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
        $data = array('file' => new \CURLFile(realpath($path)));//>=5.5
    } else {
        if (defined('CURLOPT_SAFE_UPLOAD')) {
            curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
        }
        $data = array('file' => '@' . realpath($path));//<=5.5
    }

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1 );
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
    $result = curl_exec($curl);
    $error = curl_error($curl);dump($data);
    return $result;
}

function format_ver($ver)
{
    $arr = explode('.',$ver);
    if(count($arr) > 2){
        return $arr[0].'.'.$arr[1];
    } elseif(count($arr) == 1) {
        $return = $arr[0] != 0 ? $arr[0].'.0' : $arr[0];
        return $return;
    } else {
        return $ver;
    }
}